package com.ruoyi.crop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;

import com.ruoyi.crop.api.domain.AroundTeam;
import com.ruoyi.crop.api.domain.TeamsGroups;
import com.ruoyi.crop.service.AroundTeamService;
import com.ruoyi.crop.service.TeamsGroupsService;
import com.ruoyi.system.api.domain.SysUser;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("team")
public class TeamGroupController extends BaseController {
    @Autowired
    TeamsGroupsService teamsGroupsService;

    @Autowired
    AroundTeamService aroundTeamService;
    @GetMapping("/list")
    public TableDataInfo list(TeamsGroups teamsGroups)
    {
        startPage();
        QueryWrapper<TeamsGroups> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(StringUtils.isNotBlank(teamsGroups.getTeamName()),TeamsGroups::getTeamName,teamsGroups.getTeamName());
        wrapper.lambda().eq(StringUtils.isNotBlank(teamsGroups.getTypeId()),TeamsGroups::getTypeId,teamsGroups.getTypeId());
        wrapper.lambda().eq(StringUtils.isNotBlank(teamsGroups.getTeamCode()),TeamsGroups::getTeamCode,teamsGroups.getTeamCode());
        List<TeamsGroups> list = teamsGroupsService.list(wrapper);
        return getDataTable(list);
    }


    /**
     * 获取质检详细信息
     */

    @RequestMapping("getInfo")
    public AjaxResult getInfo(Integer teamId)
    {
        return success(teamsGroupsService.getById(teamId));
    }

    /**
     * 新增班组
     */

    @PostMapping
    public AjaxResult add(@RequestBody TeamsGroups teamsGroups)
    {
        return toAjax(teamsGroupsService.save(teamsGroups));
    }

    /**
     * 修改班组
     */
    @PutMapping
    public AjaxResult edit(@RequestBody TeamsGroups teamsGroups)
    {
        return toAjax(teamsGroupsService.updateById(teamsGroups));
    }

    /**
     * 删除零售
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for (Long id : ids) {
            teamsGroupsService.removeById(id);
        }
       return success();
    }

    @PostMapping("addbanzu")
    public AjaxResult addbanzu(Integer[] ids,String bancid){
        // 先检查是否已经存在该 bancid 的数据
        AroundTeam existingTeam = aroundTeamService.getbyIds(bancid);
        if (existingTeam != null) {
            // 如果已经存在，可以根据需求选择是直接返回错误信息，还是更新已有数据等操作
            return AjaxResult.error("一个班次只能添加一个班组,不能重复添加");
        }

        // 如果不存在，可以继续添加数据
        for (Integer id : ids) {
            AroundTeam aroundTeam = new AroundTeam();
            aroundTeam.setTeamId(id);
            aroundTeam.setAroundId(bancid);

            aroundTeamService.save(aroundTeam);
        }
        return AjaxResult.success();
    }
}
